//
//  NearByViewController.m
//  XiJianChui
//
//  Created by aaa on 16/7/18.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//

#import "NearByViewController.h"
#import "QhFourButtonsCell.h"
#import "QhPictureTextIntroductionCell.h"
#import "PushRequireViewController.h"
#import "ShowGoodsViewController.h"
#import "NearByStylistViewController.h"
#import "NearByBusinessViewController.h"
#import "showGoodCollectionViewCell.h"
#import "recommendCollectionReusableView.h"

static NSInteger const cols = 1;
static CGFloat const margin = 0;
static NSString * const fourButtonsCellId = @"fourButtonsCell";
static NSString * const pictureTextIntroductionCellId = @"pictureTextIntroductionCell";
static NSString * const showGoodID = @"showGoodCollectionViewCell";
static NSString * const recommendID = @"recommend";
#define HeightForCollectionViewHeaderView 55
//#define itemWH (QhScreenW - (cols - 1) * margin) / cols
#define itemW (QhScreenW - (cols - 1) * margin) / cols
#define itemH itemW * 0.8
@interface NearByViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
//推荐商品
@property (nonatomic, weak) UICollectionView *recommendGoodsView;
@end

@implementation NearByViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 100;
    self.view.backgroundColor = QhCommonColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.title = @"附近";
    [self createRecommendGoods];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([QhFourButtonsCell class]) bundle:nil] forCellReuseIdentifier:fourButtonsCellId];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PushToPushRequireViewController) name:@"pushRequireBtnClick" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PushToShowGoodsViewController) name:@"showGoodsViewControllerBtnClick" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PushToNearByStylistViewController) name:@"nearByStylistViewControllerBtnClick" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PushToNearByBusinessViewController) name:@"nearByBusinessViewControllerBtnClick" object:nil];
}

#pragma mark - Create UI
- (void)createRecommendGoods
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置cell尺寸
    layout.itemSize = CGSizeMake(itemW, itemH);
    layout.minimumInteritemSpacing = margin;
    layout.minimumLineSpacing = margin;
    layout.headerReferenceSize = CGSizeMake(QhScreenW, HeightForCollectionViewHeaderView);
    // 创建UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 1900) collectionViewLayout:layout];
    _recommendGoodsView = collectionView;
    collectionView.backgroundColor = self.tableView.backgroundColor;
    
    self.tableView.tableFooterView = collectionView;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.scrollEnabled = NO;
    
    // 注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"showGoodCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:showGoodID];
    
    [collectionView registerNib:[UINib nibWithNibName:@"recommendCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendID];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

- (void)PushToPushRequireViewController
{
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:NSStringFromClass([PushRequireViewController class]) bundle:nil];
//    // 加载箭头指向控制器
//    PushRequireViewController *pRVc = [storyboard instantiateInitialViewController];
    PushRequireViewController *pRVc = [[PushRequireViewController alloc] init];
    [self.navigationController pushViewController:pRVc animated:YES];
}

- (void)PushToShowGoodsViewController
{
    ShowGoodsViewController *sGVc = [[ShowGoodsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:sGVc animated:YES];
    
}

- (void)PushToNearByStylistViewController
{
    NearByStylistViewController *nBSVc = [[NearByStylistViewController alloc] init];
    [self.navigationController pushViewController:nBSVc animated:YES];
    
}

- (void)PushToNearByBusinessViewController
{
    NearByBusinessViewController *nBBVc = [[NearByBusinessViewController alloc] init];
    [self.navigationController pushViewController:nBBVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
#pragma mark - tableview dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QhFourButtonsCell *cell = [tableView dequeueReusableCellWithIdentifier:fourButtonsCellId];
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return QhMarin;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return QhMarin;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    showGoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:showGoodID forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"faxing"];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    recommendCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendID forIndexPath:indexPath];
    
    return headerView;
}
@end
